package com.weixin.restapi;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.weixin.pojo.ProductCategory;
import com.weixin.pojo.ProductInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.weixin.service.ProductCategoryService;
import com.weixin.service.ProductInfoService;
import com.weixin.util.ResultVoUtil;
import com.weixin.vo.ProductInfoVo;
import com.weixin.vo.ProductVo;
import com.weixin.vo.ResultVo;

/**
 * @author CJunGod 1038804380@qq.com
 *
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductApi {
	
	@Resource
	private ProductInfoService productInfoService;
	
	@Resource
	private ProductCategoryService productCategoryService;

	@GetMapping("/list")
	public ResultVo list() {
		List<ProductInfo> findUpAll = productInfoService.findUpAll();
		
		List<Integer> categoryTypeList = findUpAll.stream().map(e -> e.getCategoryType()).
				collect(Collectors.toList());
		
		List<ProductCategory> categoryTypeIn = productCategoryService.findByCategoryTypeIn(categoryTypeList);
		
		List<ProductVo> productVos = new ArrayList<ProductVo>();
		for(ProductCategory category : categoryTypeIn) {
			ProductVo productVo = new ProductVo();
			productVo.setCategoryName(category.getCategoryName());
			productVo.setCategoryType(category.getCategoryType());
			
			List<ProductInfoVo> productInfoVOList= new ArrayList<ProductInfoVo>();
			for(ProductInfo info : findUpAll) {
				if(info.getCategoryType().equals(category.getCategoryType())) {
					ProductInfoVo infoVo = new ProductInfoVo();
					BeanUtils.copyProperties(info, infoVo);
					productInfoVOList.add(infoVo);
				}
			}
			productVo.setProductInfoVOList(productInfoVOList);
			productVos.add(productVo);
		}
		
		return ResultVoUtil.success(productVos);
	}
}
